package com.my.main.controller.sysConfig;


/**
 * 系统配置--编码库
 *
 * @author liuhx 2020-04-29
 */


import com.my.main.base.controller.BaseController;
import com.my.main.base.entity.Result;
import com.my.main.base.entity.ResultCode;
import com.my.main.common.utils.CurrencyUtil;
import com.my.main.entity.sysConfig.SysConfigCode;
import com.my.main.service.sysConfig.SysConfigCodeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value="/api/v1/sys/SysConfigCode")
public class SysConfigCodeController extends BaseController {


    @Autowired
    private SysConfigCodeService sysConfigCodeService;


    /**
     * 查询 - 系统配置--编码库列表查询
     *
     * @param map(page: 页数, limit: 每页条数)
     *
     * @author liuhx 2020-04-28
     */
    @ApiOperation(value="系统配置 - 编码库列表查询")
    @RequestMapping(value = "/querySysConfigCodeList", method = RequestMethod.POST)
    public Result querySysConfigList(@RequestBody Map<String, Object> map) {

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("page", map.get("page"));
        resMap.put("limit", map.get("limit"));

        try {
            List<Map<String, Object>> sysConfigCodeList = new ArrayList<>();
            map.put("page", CurrencyUtil.PageRowNum(Integer.parseInt(map.get("page").toString()), Integer.parseInt(map.get("limit").toString())));
            List<SysConfigCode> sysConfigCodes = sysConfigCodeService.queryList(map);

            for(SysConfigCode sysConfigCode: sysConfigCodes) {
                Map<String, Object> sysConfigCodeMap = new HashMap<>();
                // ID
                sysConfigCodeMap.put("codeId", sysConfigCode.getCodeId());
                // 码表key
                sysConfigCodeMap.put("codeKey", sysConfigCode.getCodeKey());
                // 编码说明
                sysConfigCodeMap.put("label", sysConfigCode.getLabel());
                // 组装数据
                sysConfigCodeList.add(sysConfigCodeMap);
            }

            resMap.put("sysConfigCodeList", sysConfigCodeList);
            return new Result(ResultCode.SUCCESS, resMap);

        } catch (Exception e) {
            return new Result(ResultCode.FAIL, "系统错误，请联系管理员");
        }

    }


    /**
     *  删除- 系统配置--编码库修改删除
     *
     * @param map(page: 页数, limit: 每页条数)
     *
     * @author liuhx 2020-04-28
     */
    @ApiOperation(value="系统配置 - 编码库删除")
    @RequestMapping(value = "/deleteSysConfigCode", method = RequestMethod.POST)
    public Result deleteSysConfigCode(@RequestBody Map<String, Object> map) {

        if (!StringUtils.isEmpty(map.get("codeKey"))) {

            try {
                // 通过编码key 删除 编码
                sysConfigCodeService.isDeleteByCodeKey(map.get("codeKey").toString());
                return new Result(ResultCode.SUCCESS);
            } catch (Exception e) {
                return new Result(ResultCode.FAIL);
            }
        } else {
            return new Result(ResultCode.FAIL, "码表Key不能为空！");
        }
    }


    /**
     *  新增- 系统配置--编码库新增
     *
     * @param map(page: 页数, limit: 每页条数)
     *
     * @author liuhx 2020-04-28
     */
    @ApiOperation(value="系统配置 - 编码库新增")
    @RequestMapping(value = "/saveSysConfigCode", method = RequestMethod.POST)
    public Result saveSysConfigCode(@RequestBody Map<String, Object> map) {

        String[] val = null;
        String[] txt = null;
        if (!StringUtils.isEmpty(map.get("enumVal"))) {
            val = map.get("enumVal").toString().split("\\r?\\n");
        }
        if (!StringUtils.isEmpty(map.get("enumTxt"))) {
            txt = map.get("enumTxt").toString().split("\\r?\\n");
        }
        if (val.length != txt.length) {
            return new Result(ResultCode.FAIL, "编码值与编码名称不对应！");
        }

        if (!StringUtils.isEmpty(map.get("oldCodeKey"))) {
            // 编辑
            sysConfigCodeService.isDeleteByCodeKey(map.get("oldCodeKey").toString());
        }
            // 保存操作
        List<SysConfigCode> sysConfigCodeList = new ArrayList<>();
        SysConfigCode sysConfigCode_t;

        try {
            for (int i=0; i< val.length; i++) {
                sysConfigCode_t =  new SysConfigCode();
                sysConfigCode_t.setCodeId(idWorker.nextId() + "");
                sysConfigCode_t.setCodeKey(map.get("codeKey").toString());
                sysConfigCode_t.setLabel(map.get("label").toString());
                sysConfigCode_t.setEnumVal(val[i]);
                sysConfigCode_t.setEnumTxt(txt[i]);
                sysConfigCodeList.add(sysConfigCode_t);
            }
            sysConfigCodeService.isSave(sysConfigCodeList);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAIL,"系统错误，请联系管理员!");
        }
    }


    /**
     *  新增- 系统配置--编码库查询
     *
     * @param map(page: 页数, limit: 每页条数)
     *
     * @author liuhx 2020-04-28
     */
    @ApiOperation(value="系统配置 - 编码库查询")
    @RequestMapping(value = "/querySysConfigCodeByKey", method = RequestMethod.POST)
    public Result querySysConfigCodeByKey(@RequestBody Map<String, Object> map) {

        Map<String, Object> resMap = new HashMap<>();

        try {
            List<Map<String, Object>> sysConfigCodeList = new ArrayList<>();
            List<SysConfigCode> sysConfigCodes = sysConfigCodeService.queryListByCodeKey(map.get("codeKey").toString());
            for (SysConfigCode sysConfigCode : sysConfigCodes) {
                Map<String, Object> sysConfigCodeMap = new HashMap<>();
                sysConfigCodeMap.put("label", sysConfigCode.getLabel());
                sysConfigCodeMap.put("codeKey", sysConfigCode.getCodeKey());
                sysConfigCodeMap.put("enumVal", sysConfigCode.getEnumVal());
                sysConfigCodeMap.put("enumTxt", sysConfigCode.getEnumTxt());
                sysConfigCodeList.add(sysConfigCodeMap);
            }
            resMap.put("sysConfigCodeList", sysConfigCodeList);
            return new Result(ResultCode.SUCCESS, resMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAIL,"系统错误，请联系管理员");
        }
    }
}
